﻿using System;
using System.Data;
using System.Drawing;
using System.Collections.Generic;

namespace Common.Modules
{
    public class GraphData
    {
        public AnalyticsType Type { get; set; }

        public string Title { get; set; }

        public string XAxisTitle { get; set; }

        public string YAxisTitle { get; set; }

        public IList<double[]> XAxisDatasList { get; set; }

        public IList<double[]> YAxisDatasList { get; set; }

        /// <summary>
        /// 折线或Bar或饼的各个子单元的名称
        /// </summary>
        public string[] XNames { get; set; }
        /// <summary>
        /// 多折线或Bar的分类名称
        /// </summary>
        public string[] TypeNames { get; set; }
    }

    public class GraphMaker
    {
        private readonly ZedGraphPainter _zg = new ZedGraphPainter();
        private GraphData _graphData;

        public Image Draw(GraphData graphData)
        {
            _graphData = graphData;
            switch (graphData.Type)
            {
                case AnalyticsType.Bar:
                    DrawBar();
                    break;
                case AnalyticsType.Line:
                    DrawLine();
                    break;
                case AnalyticsType.Pie:
                    DrawPie();
                    break;
            }
            return _zg.GenerateImage();
        }


        public Image Draw(DataTable table, DataColumn typeColumn, DataColumn[] dataColumns,
            string dataName, AnalyticsType type)
        {
            IList<double[]> XAxisDatasList = new List<double[]>();
            IList<double[]> YAxisDatasList = new List<double[]>();
            List<string> XNames = new List<string>();
            List<string> TypeNames = new List<string>();
            foreach (DataRow row in table.Rows)
            {
                TypeNames.Add(row[typeColumn] as string);
            }
            for (int i = 0; i < dataColumns.Length; i++)
            {
                var column = dataColumns[i];
                XNames.Add(column.ColumnName);
                double[] xdatas = new double[table.Rows.Count];
                double[] ydatas = new double[table.Rows.Count];
                for (int j = 0; j < table.Rows.Count; j++)
                {
                    DataRow row = table.Rows[j];
                    double rv;
                    if (double.TryParse(row[column] as string, out rv))
                    {
                        ydatas[table.Rows.IndexOf(row)] = rv;
                    }
                    else
                    {
                        ydatas[table.Rows.IndexOf(row)] = 0;
                    }
                    xdatas[table.Rows.IndexOf(row)] = 3 + j * dataColumns.Length + 5 + j + 2;
                }
                XAxisDatasList.Add(xdatas);
                YAxisDatasList.Add(ydatas);
            }
            _graphData = new GraphData()
            {
                Type = type,
                Title = table.TableName,
                XAxisTitle = typeColumn.ColumnName,
                YAxisTitle = dataName,
                XAxisDatasList = XAxisDatasList,
                YAxisDatasList = YAxisDatasList,
                XNames = XNames.ToArray(),
                TypeNames = TypeNames.ToArray()
            };
            return Draw(_graphData);
        }

        private void DrawBar()
        {
            _zg.Type = AnalyticsType.Bar;
            _zg.Title = _graphData.Title;
            _zg.XAxisTitle = _graphData.XAxisTitle;
            _zg.YAxisTitle = _graphData.YAxisTitle;
            for (int i = 0; i < _graphData.XAxisDatasList.Count; i++)
            {
                ZedGraph.PointPairList ppl = new ZedGraph.PointPairList();
                ppl.Add(_graphData.XAxisDatasList[i], _graphData.YAxisDatasList[i]);
                _zg.DataSource.Add(ppl);
                _zg.NameList.AddRange(_graphData.XNames);
            }
            _zg.Draw();
        }

        private void DrawPie()
        {
            _zg.Type = AnalyticsType.Pie;
            _zg.Title = _graphData.Title;
            var datas = _graphData.XAxisDatasList[0];
            _zg.ScaleData.AddRange(datas);
            _zg.NameList.AddRange(_graphData.XNames);
            _zg.Draw();
        }

        private void DrawLine()
        {
            _zg.Type = AnalyticsType.Line;
            _zg.Title = _graphData.Title;
            _zg.XAxisTitle = _graphData.XAxisTitle;
            _zg.YAxisTitle = _graphData.YAxisTitle;
            for (int i = 0; i < _graphData.XAxisDatasList.Count; i++)
            {
                ZedGraph.PointPairList ppl = new ZedGraph.PointPairList();
                ppl.Add(_graphData.XAxisDatasList[i], _graphData.YAxisDatasList[i]);
                _zg.DataSource.Add(ppl);
                _zg.NameList.Add(_graphData.TypeNames[i]);
            }
            _zg.Draw();
        }
    }
}